package cn.pconline.autoclub.entity;

import cn.pconline.autoclub.service.PromotionService;
import cn.pconline.autoclub.util.DateUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.gelivable.dao.Column;
import org.gelivable.dao.Entity;
import org.gelivable.dao.Id;
import org.gelivable.dao.Transient;
import org.gelivable.web.EnvUtils;

/**
 * @author fengguosen
 * @email fengguosen@pconline.com.cn 创建时间：2014-10-14 优惠活动
 */
@Entity(tableName = "ac_promotion")
public class Promotion {
    @Id
    @Column(name = "promotion_id")
    private long promotionId;
    private String name;
    private String url;
    private String logo;
    private String introduce;
    @Column(name = "start_date")
    private Date startDate;      
    @Column(name = "end_date")
    private Date endDate;       
    @Column(name = "sort_weight")
    private int sortWeight;
    @Column(name = "create_by")
    private long createBy;
    @Column(name = "update_by")
    private long updateBy;
    @Column(name = "create_at")
    private Date createAt;
    @Column(name = "update_at")
    private Date updateAt;
    @Column(name = "signup_count")
    private int signUpCount;
    private int status; // '状态(-1 删除 ，0 正常)
    public static final int STATUS_DELETE = -1;//删除
    public static final int STATUS_NORMAL = 0;//正常   
    
    public static final int TIME_STATUS_DELETE = -1;//删除
    public static final int TIME_STATUS_ALL = 0;//全部
    public static final int TIME_STATUS_PREPARE = 1;//未开始
    public static final int TIME_STATUS_UNDER = 2;//进行中
    public static final int TIME_STATUS_END = 3;//已结束
    public static final HashMap<Integer, String> statusDesc = new HashMap<Integer, String>();
    public final static long[] proIds = {110000,120000,310000,500000,710000,810000,820000};  //分别是 ：北京，天津，上海，重庆，台湾，香港，澳门
    public static final List<Long> specialProIds = new ArrayList<Long>();
    static {
        statusDesc.put(TIME_STATUS_ALL, "全部");
        statusDesc.put(TIME_STATUS_PREPARE, "未开始");
        statusDesc.put(TIME_STATUS_UNDER, "进行中");
        statusDesc.put(TIME_STATUS_END, "已结束");
        for(long proId :proIds){
        	specialProIds.add(proId);
        }
    }
    
    private PromotionPublish promotionPublish;
    @Transient
    private String pushRange;
    
    public String getPushRange() {
		return pushRange;
	}

	public void setPushRange(String pushRange) {
		this.pushRange = pushRange;
	}

	public long getPromotionId() {
        return promotionId;
    }

    public void setPromotionId(long promotionId) {
        this.promotionId = promotionId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public String getIntroduce() {
        return introduce;
    }

    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public int getSortWeight() {
        return sortWeight;
    }

    public void setSortWeight(int sortWeight) {
        this.sortWeight = sortWeight;
    }

    public long getCreateBy() {
        return createBy;
    }

    public void setCreateBy(long createBy) {
        this.createBy = createBy;
    }

    public long getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(long updateBy) {
        this.updateBy = updateBy;
    }

    public Date getCreateAt() {
        return createAt;
    }

    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

    public Date getUpdateAt() {
        return updateAt;
    }

    public void setUpdateAt(Date updateAt) {
        this.updateAt = updateAt;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

	public int getSignUpCount() {
		return signUpCount;
	}

	public void setSignUpCount(int signUpCount) {
		this.signUpCount = signUpCount;
	}

	public PromotionPublish getPromotionPublish() {
        if (promotionId <= 0) {
            return null;
        }
        if (promotionPublish != null) {
            return promotionPublish;
        }
        List<PromotionPublish> list = EnvUtils.getEnv().getBean(PromotionService.class).listBypromotionId(promotionId);
        if (list != null && list.size() > 0) {
            setPromotionPublish(list.get(0));
            return list.get(0);
        }
        return null;
    }

    public void setPromotionPublish(PromotionPublish promotionPublish) {
        this.promotionPublish = promotionPublish;
    }

    /**
     * @Description: 根据当前时间获取优惠活动状态
     * @return: int
     */
    public int getTimeStatus() {
        if (status < Promotion.STATUS_NORMAL) {
            return Promotion.TIME_STATUS_DELETE;  //已删除
        } else {
            Date nowDate = DateUtils.parseDate(DateUtils.format(DateUtils.getNow()), "yyyy-MM-dd");
            PromotionPublish proPublish = getPromotionPublish();
            if (proPublish != null) {
                Date sDate = DateUtils.parseDate(DateUtils.format(proPublish.getStartDate()), "yyyy-MM-dd");
                Date eDate = DateUtils.parseDate(DateUtils.format(proPublish.getEndDate()), "yyyy-MM-dd");
                if (sDate != null && eDate != null) {
                    if (nowDate.getTime() < sDate.getTime()) {
                        return Promotion.TIME_STATUS_PREPARE;  //未开始
                    } else {
                        if (nowDate.getTime() <= eDate.getTime()) {
                            return Promotion.TIME_STATUS_UNDER; //进行中
                        } else {
                            return Promotion.TIME_STATUS_END;//已结束
                        }
                    }
                }
            }
            return Promotion.TIME_STATUS_DELETE; // 不存在PromotionPublish
        }
    }

    /**
     * 依赖URL：http://bbs.pcauto.com.cn/topic-5460623.html
     *
     * @return
     */
    public long getTopicId() {
        String s = getUrl();
        if (StringUtils.isEmpty(s)) {
            return 0;
        }
        int start = s.lastIndexOf("-");
        int end = s.lastIndexOf(".");
        if (start >= end || start == -1 || end == -1) {
            return 0;
        }
        try {
            return Long.parseLong(s.substring(start + 1, end));
        } catch (java.lang.NumberFormatException e) {
            return 0;
        }
    }
}
